﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class DriverFinishTransport : Form
    {
        int userID;
        SqlConnection dbConnection;

        public DriverFinishTransport(int id)
        {
            userID = id;
            SqlConnector connection = new SqlConnector();
            dbConnection = connection.getInstance();
            try
            {
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            InitializeComponent();
            SqlCommand command = new SqlCommand("SELECT t.start_city, t.transport_start_time, " +
                                    "t.destination_city, t.distance, w.name, w.surname " +
                                    "FROM (Transport t JOIN Workers w ON w.worker_ID = t.worker_ID)" +
                                    "WHERE t.driver_ID = (SELECT u.worker_id FROM Users u WHERE u.user_ID = " + userID + ") AND t.status = 'w trakcie'", dbConnection);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            if (reader.Read())
            {
                startPlace.Text = reader["start_city"].ToString();
                endPlace.Text = reader["destination_city"].ToString();
                startTime.Text = reader["transport_start_time"].ToString();
                distance.Text = reader["distance"].ToString();
                dispatcher.Text = reader["name"].ToString() + " " + reader["surname"].ToString();
            }
            reader.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand("SELECT transport_ID FROM Transport WHERE status = 'w trakcie' AND driver_ID = (SELECT worker_ID FROM Users WHERE user_ID = " + userID + ")", dbConnection);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            reader.Read();
            int transportID = int.Parse(reader["transport_ID"].ToString());
            reader.Close();
            //zakończenie transportu, zmiana statusów paczek
            SqlCommand updateTransport = new SqlCommand("UPDATE Transport SET status = 'zakonczony', transport_end_time = CURRENT_TIMESTAMP " +
                                                        "WHERE transport_id = (SELECT tr.transport_ID FROM Transport tr WHERE tr.transport_ID = " + transportID + ")", dbConnection);
            updateTransport.ExecuteNonQuery();

            SqlCommand checkTransport = new SqlCommand("SELECT start_city FROM Transport WHERE transport_ID = " + transportID, dbConnection);
            SqlDataReader check = null;
            check = checkTransport.ExecuteReader();
            check.Read();
            String newStatus = "sortownia";
            if (check["start_city"].ToString() == "sortownia") newStatus = "zakonczony";
            check.Close();
            SqlCommand updatePackages = new SqlCommand("UPDATE Package SET status = '" + newStatus + "' " +
                                                       "WHERE transport_id = " + transportID, dbConnection);
            updatePackages.ExecuteNonQuery();
            MessageBox.Show("Transport zakończono poprawnie!");
            this.Hide();
        }
    }
}